#!/bin/busybox sh

# Copyright 2020 sbct
# Distributed under the terms of the GNU General Public License v3

# Basic functions and variables

cryptOpts="--tries 5 luksOpen --header /mnt/head/head.img"
cryptOpts2="--header /mnt/root/root/pee.png -d /mnt/root/root/fart.png"

rescue_shell() {
  setsid sh -c 'exec sh </dev/tty1 >/dev/tty1 2>&1'
  eerror "something failed, dropping to a shell"
  busybox --install -s
  exec /bin/sh
}

chkHead() {
  while [ -z $(findfs UUID="4a6fa337-0586-4790-86fd-869f3510aeaf") ]
  do
     echo cu > /dev/null
  done
}

findRoot() {
  clear && blkid
  eerror "Root partition not found, specify it:"
  read rootPart
  cryptsetup $cryptOpts $rootPart enema
}
findData() {
  clear && blkid
  eerror "Data partition not found, specify it:"
  read dataPart
  cryptsetup $cryptOpts2 $dataPart urine
}


# Minimalist implementation of Gentoo's einfo/ebegin/eend/etc message functions
# Does not include: syslog support, any configuration settings, indentation, right-justified eend
# (C) Stephen Cavilia, 2020

# hard-coded standard color codes (skip tput detection)
GOOD=$(printf '\033[32;01m')
WARN=$(printf '\033[33;01m')
BAD=$(printf '\033[31;01m')
NORMAL=$(printf '\033[0m')

# Display an informational message
einfo()
{
    echo " ${GOOD}*${NORMAL} $*"
}

# Display a warning message
ewarn()
{
    echo " ${WARN}*${NORMAL} $*"
}

# Display an erorr message, and return failure
eerror()
{
    echo " ${BAD}*${NORMAL} $*"
    return 1
}

# Start an operation, displaying a message
ebegin()
{
    echo -n " ${GOOD}*${NORMAL} $* ... "
}

# Display the status of the operation started with ebegin
eend()
{
    if [ "${1:-0}" = "0" ]; then
        echo "${GOOD}[ ok ]${NORMAL}"
    else
        echo "${BAD}[ !! ]${NORMAL}"
    fi
}
